apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: {{ template "name" . }}-client
  labels:
    app: {{ template "name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
    component: {{ template "name" . }}
    role: client
spec:
  replicas: {{ .Values.client.replicas }}
  template:
    metadata:
      labels:
        component: {{ template "name" . }}
        role: client
    spec:
      {{- if eq .Values.client.antiAffinity "hard" }}
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - topologyKey: "kubernetes.io/hostname"
              labelSelector:
                matchLabels:
                  component: {{ template "name" . }}
                  role: client
      {{- else if eq .Values.client.antiAffinity "soft" }}
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 1
            podAffinityTerm:
              topologyKey: "kubernetes.io/hostname"
              labelSelector:
                matchLabels:
                  component: {{ template "name" . }}
                  role: client
      {{- end }}

      initContainers:
      - name: init-sysctl
        image: "{{ .Values.image.init.repository }}:{{ .Values.image.init.tag }}"
        imagePullPolicy: {{ .Values.image.init.pullPolicy }}
        command: ["sysctl", "-w", "vm.max_map_count=262144"]
        securityContext:
          privileged: true

      containers:
      - name: es-client
        securityContext:
          privileged: false
          capabilities:
            add:
              - IPC_LOCK
              - SYS_RESOURCE
        image: "{{ .Values.image.es.repository }}:{{ .Values.image.es.tag }}"
        imagePullPolicy: {{ .Values.image.es.pullPolicy }}
        env:
        - name: NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: DISCOVERY_SERVICE
          value: {{ template "name" . }}-discovery
        {{- range $key, $value :=  .Values.common.env }}
        - name: {{ $key | upper | replace "-" "_" }}
          value: {{ $value | quote }}
        {{- end }}
        {{- range $key, $value :=  .Values.client.env }}
        - name: {{ $key | upper | replace "-" "_" }}
          value: {{ $value | quote }}
        {{- end }}
        - name: "ES_JAVA_OPTS"
          value: "-Xms{{ .Values.client.heapMemory }} -Xmx{{ .Values.client.heapMemory }}"
        resources:
{{ toYaml .Values.client.resources | indent 10 }}
        ports:
        - containerPort: 9200
          name: http
          protocol: TCP
        - containerPort: 9300
          name: transport
          protocol: TCP
        livenessProbe:
          tcpSocket:
            port: 9300
          initialDelaySeconds: 30
        readinessProbe:
          httpGet:
            path: /_cluster/health
            port: 9200
          initialDelaySeconds: 20
          timeoutSeconds: 5
        volumeMounts:
        - name: storage
          mountPath: /data
      volumes:
          - emptyDir:
              medium: ""
            name: "storage"
      nodeSelector:
{{ toYaml .Values.common.nodeSelector | indent 8 }}
